//先定义三个常量表示状态
const PENDING = 'pending',
	FULFILLED = 'fulfilled',
	REJECTED = 'rejected';
//新建MyPromise 类
class MyPromise {
	constructor(executor){
		//executor 是一个执行器，进入会立即执行，并传入resolve 和reject 方法
		executor(this.resolve,this.reject)
	}
	//储存状态的变量，初始值是 pending
	status = PENDING;
	//成功之后的值
	value = null;
	//失败之后的值
	reason = null;
	
	//更改成功后的状态
	resolve = (value) => {
		if(this.status === PENDING){
			this.status = FULFILLED;
			this.value = value
		}
	}
	reject = (reason) => {
		if(this.status === PENDING){
			this.status = REJECTED;
			this.reason = reason;
		}
	}
	
	//then 的实现
	then(onFulfilled,onRejected) {
		//判断状态
		if(this.status === FULFILLED){
			//调用成功回调，并且把值返回
			onFulfilled(this.value)
		}else if (this.status === REJECTED) {
			//调用成功失败，并且把值返回
			onRejected(this.reason)
		}
	}
}
module.exports = MyPromise;